package org.example.completed.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.completed.domain.Comment;
import org.example.completed.mapper.CommentMapper;
import org.example.completed.model.dto.CommentAndAvatarDTO;
import org.example.completed.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private final CommentMapper commentMapper;
    @Autowired
    UserServiceImpl userService;

    @Autowired
    public CommentServiceImpl(CommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }

    @Override
    public List<Comment> getAllComments() {
        return commentMapper.getAllComments();
    }

    @Override
    public void addComment(Comment comment) {
        commentMapper.addComment(comment);
    }

    @Override
    public CommentAndAvatarDTO getCommentsByUsernameWithAvatar(String username) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<Comment>()
                .eq(Comment::getUsername,username);
        List<Comment> comments = commentMapper.selectList(wrapper);

        String img = userService.getImgByUsername(username);

        CommentAndAvatarDTO commentAndAvatarDTO = new CommentAndAvatarDTO();
        commentAndAvatarDTO.setCommentList(comments);
        commentAndAvatarDTO.setImg(img);

        return commentAndAvatarDTO;
    }
}